//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/buola.h>
#include <buola/utility/usignal.h>
#include <buola/io.h>

struct CEmitter
{
    CEmitter()
    {
        
    }
    
    void Emit(int pCount)
    {
        for(int i=0;i<pCount;i++)
            sSignal(i);
    }

    buola::USignal<void(int)> sSignal;
};

struct CReceiver
{
    CReceiver()
        :   mValue(0)
    {
        
    }

    virtual ~CReceiver()
    {
        
    }

    virtual void Receive(int i)
    {
        mValue+=i;
    }
    
    uint64_t mValue;
};

struct CReceiverD : public CReceiver
{
    virtual void Receive(int i)
    {
        mValue+=2*i;
    }
};

int main()
{
    using namespace buola;
    
    CEmitter lEmitter;
    CReceiver *lReceiver=new CReceiverD;
    
    start_timer();
    for(int i=0;i<10;i++)
//        lEmitter.sSignal.Connect([=](int i){lReceiver->Receive(i);});
        lEmitter.sSignal.Connect(&CReceiver::Receive,lReceiver,_1);
    end_timer();
    
    start_timer();
    lEmitter.Emit(10000000);
    end_timer();
    start_timer();
    lEmitter.Emit(10000000);
    end_timer();
    
    start_timer();
    for(int i=0;i<10000000;i++)
    {
//        USlot<void(int)> *lSlot=lEmitter.sSignal.Connect([=](int i){lReceiver->Receive(i);});
        USlot<void(int)> *lSlot=lEmitter.sSignal.Connect(&CReceiver::Receive,lReceiver,_1);
        if(!(i%20))
            lSlot->mDeleted=true;
    }
    end_timer();
    
    msg_info() << lEmitter.sSignal.Count() << "\n";
    
    start_timer();
    lEmitter.Emit(10);
    end_timer();
    start_timer();
    lEmitter.Emit(10);
    end_timer();
    
    msg_info() << lEmitter.sSignal.Count() << "\n";

    msg_info() << lReceiver->mValue << "\n";
}
